Passed
Push — master ( 33dd36...c64a01 )
by René
03:33
created

vote.js ➔ ... ➔ $(ꞌ.result-cell.noꞌ).each   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 3
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
/** global: Clipboard */
2
var newUserDates = [];
3
var newUserTypes = [];
4
5
var maxVotes = 0;
6
var valuesChanged = false;
7
8
var tzOffset = new Date().getTimezoneOffset();
9
10
$.fn.switchClass = function (a, b) {
11
	this.removeClass(a);
12
	this.addClass(b);
13
	return this;
14
};
15
16
function updateCommentsCount() {
17
	$('#comment-counter').removeClass('no-comments');
18
	$('#comment-counter').text(parseInt($('#comment-counter').text()) +1);
19
20
}
21
22
function updateBest() {
23
	maxVotes = 0;
24
	$('.counter').each(function () {
25
		var yes = parseInt($(this).find('.yes').text());
26
		var no = parseInt($(this).find('.no').text());
27
		if(yes - no > maxVotes) {
28
			maxVotes = yes - no;
29
		}
30
	});
31
32
	$('.vote').each(function () {
33
		var yes = parseInt($(this).find('.yes').text());
34
		var no = parseInt($(this).find('.no').text());
35
		$(this).toggleClass('winner', yes - no === maxVotes);
36
	});
37
}
38
39
function updateCounters() {
40
	$('.result-cell.yes').each(function () {
41
		$(this).text($('#voteid_'+ $(this).attr('data-voteId') + '.poll-cell.yes').length);
42
	});
43
	$('.result-cell.no').each(function () {
44
		$(this).text($('#voteid_'+ $(this).attr('data-voteId') + '.poll-cell.no').length);
45
	});
46
	updateBest();
47
}
48
49
function updateAvatar(obj) {
50
	// Temporary hack - Check if we have Nextcloud or ownCloud with an anomymous user
51
	if (!document.getElementById('nextcloud') && OC.currentUser === '') {
52
		$(obj).imageplaceholder(obj.title);
53
	} else {
54
		$(obj).avatar(obj.title, 32);
55
	}
56
}
57
58
function switchSidebar() {
59
	if ($('#app-content').hasClass('with-app-sidebar')) {
60
		OC.Apps.hideAppSidebar();
61
	} else {
62
		OC.Apps.showAppSidebar();
63
	}
64
}
65
66
$(document).ready(function () {
67
	var clipboard = new Clipboard('.copy-link');
0 ignored issues
show
Bug introduced by
Clipboard does not seem to be defined.
Loading history...
68
	clipboard.on('success', function(e) {
69
		var $input = $(e.trigger);
70
		$input.tooltip('hide')
71
			.attr('data-original-title', t('core', 'Copied!'))
72
			.tooltip('fixTitle')
73
			.tooltip({placement: 'bottom', trigger: 'manual'})
74
			.tooltip('show');
75
		_.delay(function() {
76
			$input.tooltip('hide');
77
			if (OC.Share.Social.Collection.size() === 0) {
78
				$input.attr('data-original-title', t('core', 'Copy'))
79
					.tooltip('fixTitle');
80
			} else {
81
				$input.tooltip('destroy');
82
			}
83
		}, 3000);
84
	});
85
	clipboard.on('error', function (e) {
86
		var $input = $(e.trigger);
87
		var actionMsg = '';
88
		if (/iPhone|iPad/i.test(navigator.userAgent)) {
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
89
			actionMsg = t('core', 'Not supported!');
90
		} else if (/Mac/i.test(navigator.userAgent)) {
91
			actionMsg = t('core', 'Press ⌘-C to copy.');
92
		} else {
93
			actionMsg = t('core', 'Press Ctrl-C to copy.');
94
		}
95
96
		$input.tooltip('hide')
97
			.attr('data-original-title', actionMsg)
98
			.tooltip('fixTitle')
99
			.tooltip({placement: 'bottom', trigger: 'manual'})
100
			.tooltip('show');
101
		_.delay(function () {
102
			$input.tooltip('hide');
103
			if (OC.Share.Social.Collection.size() == 0) {
0 ignored issues
show
Bug introduced by
=== was expected, but instead == was given.
Loading history...
104
				$input.attr('data-original-title', t('core', 'Copy'))
105
					.tooltip('fixTitle');
106
			} else {
107
				$input.tooltip("destroy");
108
			}
109
		}, 3000);
110
	});
111
	// count how many times in each date
112
	updateBest();
113
114
	// Temporary hack - Check if we have Nextcloud or ownCloud with an anonymous user
115
	var hideAvatars = false;
116
	if (!document.getElementById('nextcloud')) {
117
		if (OC.currentUser === '') {
118
			hideAvatars = true;
119
		}
120
	}
121
122
	$('.delete-poll').click(function () {
123
		deletePoll(this);
0 ignored issues
show
Bug introduced by
deletePoll does not seem to be defined.
Loading history...
124
	});
125
126
	$('#switchDetails').click(function () {
127
		switchSidebar();
128
	});
129
130
	$('#closeDetails').click(function () {
131
		OC.Apps.hideAppSidebar();
132
	});
133
134
	$('.avatar').each(function (i, obj) {
135
		updateAvatar(obj);
136
	});
137
138
	$('.vote.time').each(function () {
139
		var extendedDate = new Date($(this).attr('data-value-utc').replace(/ /g,'T')+'Z'); //Fix display in Safari and IE
140
141
		$(this).find('.month').text(extendedDate.toLocaleString(window.navigator.language, {month: 'short'}));
142
		$(this).find('.day').text(extendedDate.toLocaleString(window.navigator.language, {day: 'numeric'}));
143
		$(this).find('.dayow').text(extendedDate.toLocaleString(window.navigator.language, {weekday: 'short'}));
144
		$(this).find('.time').text(extendedDate.toLocaleTimeString(window.navigator.language, {hour: 'numeric', minute:'2-digit', timeZoneName:'short'}));
145
146
	});
147
148
	$('#submit_finish_vote').click(function () {
149
		var form = document.finish_vote;
150
		var ac = document.getElementById('user_name');
151
		if (ac !== null) {
152
			if (ac.value.length >= 3) {
153
				form.elements.userId.value = ac.value;
154
			} else {
155
				alert(t('polls', 'You are not registered.\nPlease enter your name to vote\n(at least 3 characters).'));
156
				return;
157
			}
158
		}
159
		var check_notif = document.getElementById('check_notif');
160
		var newUserDates = [], newUserTypes = [];
161
		$('.poll-cell.active').each(function () {
162
			if($(this).hasClass('no')) {
163
				newUserTypes.push(0);
164
			} else if ($(this).hasClass('yes')) {
165
				newUserTypes.push(1);
166
			} else if($(this).hasClass('maybe')) {
167
				newUserTypes.push(2);
168
			} else {
169
				newUserTypes.push(-1);
170
			}
171
			if (isNaN($(this).attr('data-value'))) {
172
				newUserDates.push($(this).attr('data-value'));
173
			} else {
174
				newUserDates.push(parseInt($(this).attr('data-value')));
175
			}
176
		});
177
		form.elements.dates.value = JSON.stringify(newUserDates);
178
		form.elements.types.value = JSON.stringify(newUserTypes);
179
		form.elements.receiveNotifications.value = (check_notif && check_notif.checked) ? 'true' : 'false';
180
		form.elements.changed.value = valuesChanged ? 'true' : 'false';
181
		form.submit();
182
	});
183
184
	$('#submit_send_comment').click(function (e) {
185
		e.preventDefault();
186
		var form = document.send_comment;
187
		var ac = document.getElementById('user_name_comm');
188
		if (ac !== null) {
189
			if(ac.value.length >= 3) {
190
				form.elements.userId.value = ac.value;
191
			} else {
192
				alert(t('polls', 'You are not registered.\nPlease enter your name to vote\n(at least 3 characters).'));
193
				return;
194
			}
195
		}
196
		var comment = document.getElementById('commentBox');
197
		if(comment.textContent.trim().length <= 0) {
198
			alert(t('polls', 'Please add some text to your comment before submitting it.'));
199
			return;
200
		}
201
		var data = {
202
			pollId: form.elements.pollId.value,
203
			userId: form.elements.userId.value,
204
			commentBox: comment.textContent.trim()
205
		};
206
		$('.new-comment .icon-loading-small').show();
207
		$.post(form.action, data, function (data) {
208
			var newCommentElement = '<li class="comment flex-column"> ' +
209
									'<div class="authorRow user-cell flex-row"> ' +
210
									'<div class="avatar missing" title="' + data.userId + '"></div> ' +
211
									'<div class="author">' + data.displayName + '</div>' +
212
									'<div class="date has-tooltip live-relative-timestamp datespan" data-timestamp="' + Date.now() + '" title="' + data.date + '">' + t('polls', 'just now') + '</div>' +
213
									'</div>' +
214
									'<div class="message wordwrap comment-content">' + data.comment + '</div>' +
215
									'</li>';
216
217
218
			$('#no-comments').after(newCommentElement);
219
220
			if (!$('#no-comments').hasClass('hidden')) {
221
				$('#no-comments').addClass('hidden');
222
			}
223
224
			$('.new-comment .message').text('').focus();
225
			$('.new-comment .icon-loading-small').hide();
226
227
			$('.avatar.missing').each(function (i, obj) {
228
				// oC hack
229
				if (!hideAvatars) {
230
					$(obj).avatar(obj.title, 32);
231
				} else {
232
					$(obj).imageplaceholder(obj.title);
233
				}
234
				$(obj).removeClass('missing');
235
			});
236
237
			updateCommentsCount();
238
		}).error(function () {
239
			alert(t('polls', 'An error occurred, your comment was not posted.'));
240
			$('.new-comment .icon-loading-small').hide();
241
		});
242
	});
243
244
	$('.share input').click(function () {
245
		$(this).select();
246
	});
247
248
	$('.has-tooltip').tooltip();
249
	$('.has-tooltip-bottom').tooltip({placement:'bottom'});
250
	updateCounters();
251
252
});
253
254
$('#commentBox').keyup(function () {
255
	var $message = $('#commentBox');
256
	if(!$message.text().trim().length) {
257
		$message.empty();
258
	}
259
});
260
261
$(document).on('click', '.toggle-cell, .poll-cell.active', function () {
262
	valuesChanged = true;
263
	var $nextClass = '';
264
	var $toggleAllClasses = '';
265
266
	if ($(this).hasClass('yes')) {
267
		$nextClass = 'no';
268
		$toggleAllClasses= 'yes';
269
	} else if($(this).hasClass('no')) {
270
		$nextClass = 'maybe';
271
		$toggleAllClasses= 'no';
272
	} else if($(this).hasClass('maybe')) {
273
		$nextClass = 'yes';
274
		$toggleAllClasses= 'maybe';
275
	} else {
276
		$nextClass = 'yes';
277
		$toggleAllClasses= 'maybe';
278
	}
279
280
	$(this).removeClass('yes no maybe unvoted');
281
	$(this).addClass($nextClass);
282
283
	if ($(this).hasClass('toggle-cell')) {
284
		$('.poll-cell.active').removeClass('yes no maybe unvoted');
285
		$('.poll-cell.active').addClass($toggleAllClasses);
286
	}
287
	updateCounters();
288
});
289